package com.qs.home.ui.VM;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.luck.picture.lib.config.PictureConfig;
import com.qs.base.router.RouterActivityPath;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.entity.HomeContentEntity;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

/**
 * 主页VM
 *
 * @Author ltzz
 * @Date 2019/8/8
 */
public class HomeViewModel extends BaseViewModel {
    public HomeViewModel(@NonNull Application application) {
        super(application);
        //创建内容数据
        List<HomeContentEntity> homeContentEntities=new ArrayList<>();
        List<HomeContentEntity.ButtonEnTity> buttonEnTities=new ArrayList<>();
        homeContentEntities.add(new HomeContentEntity("扫描",null));
        buttonEnTities.add(new HomeContentEntity.ButtonEnTity("拍照识别", R.drawable.camera_blue));
        buttonEnTities.add(new HomeContentEntity.ButtonEnTity("图片识别", R.drawable.image_red_icon));
        buttonEnTities.add(new HomeContentEntity.ButtonEnTity("证件扫描", R.drawable.identity_card_icon));
        buttonEnTities.add(new HomeContentEntity.ButtonEnTity("票据扫描", R.drawable.bill_icon));
        homeContentEntities.add(new HomeContentEntity(null,buttonEnTities));
        List<HomeContentEntity.ButtonEnTity> buttonEnTities1=new ArrayList<>();
        homeContentEntities.add(new HomeContentEntity("翻译",null));
        buttonEnTities1.add(new HomeContentEntity.ButtonEnTity("拍照翻译", R.drawable.camera_red_icon));
        buttonEnTities1.add(new HomeContentEntity.ButtonEnTity("图片翻译", R.drawable.image_yellow_icon));
        buttonEnTities1.add(new HomeContentEntity.ButtonEnTity("阅读翻译", R.drawable.book_icon));
        buttonEnTities1.add(new HomeContentEntity.ButtonEnTity("文字翻译", R.drawable.text_icon));
        homeContentEntities.add(new HomeContentEntity(null,buttonEnTities1));
        List<HomeContentEntity.ButtonEnTity> buttonEnTities2=new ArrayList<>();
        homeContentEntities.add(new HomeContentEntity("其它功能",null));
        buttonEnTities2.add(new HomeContentEntity.ButtonEnTity("二维码扫描", R.drawable.rich_scan_icon));
        buttonEnTities2.add(new HomeContentEntity.ButtonEnTity("二维码生成", R.drawable.qr_code_icon));
        homeContentEntities.add(new HomeContentEntity(null,buttonEnTities2));
        homeContent.set(homeContentEntities);
    }
    //首页内容数据
    public ObservableField<List<HomeContentEntity>> homeContent = new ObservableField<>();
    //我的点击时间
    public BindingCommand myClick=new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ARouter.getInstance().build(RouterActivityPath.User.USER_MAIN).navigation();
        }
    });
    //相册是否含有拍照按钮
    public boolean havePhoto=false;
    //多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
    public int isMultiple= PictureConfig.MULTIPLE;

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //图片选择
        public ObservableBoolean selectImg=new ObservableBoolean(false);
        //照片识别
        public ObservableBoolean photoDiscernUc = new ObservableBoolean(false);
        //通用票据识别
        public ObservableBoolean noteDiscernUc = new ObservableBoolean(false);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<HomeItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<HomeItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<HomeItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, HomeItemViewModel item) {
            //设置布局
            if (!StringUtils.isTrimEmpty(item.mItemEntity.get().getTitle()))
                itemBinding.set(BR.viewModel, R.layout.home_home_title_item);
            else
                itemBinding.set(BR.viewModel, R.layout.home_home_content_item);
        }
    });
    //给RecyclerView添加Adpter，请使用自定义的Adapter继承BindingRecyclerViewAdapter，重写onBindBinding方法，里面有你要的Item对应的binding对象
    public BindingRecyclerViewAdapter<HomeItemViewModel> adapter = new BindingRecyclerViewAdapter<HomeItemViewModel>() {
        @Override
        public void onBindBinding(ViewDataBinding binding, int variableId, int layoutRes, int position, final HomeItemViewModel item) {
            super.onBindBinding(binding, variableId, layoutRes, position, item);
        }
    };
}
